// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/*
 * File:   ChunkDataPCMShort.java
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>
package uoc.libwave;

// <editor-fold defaultstate="collapsed" desc="imports">
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.List;
// </editor-fold>

/**
 * Class for storing the data chunk of a PCM S16 LE audio file in PCM S16 LE format
 * and, for reading and writing its content in Data I/O Streams.
 *
 * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * @version 1.0
 * @since 2013-08-04
 */
final class ChunkDataPCMShort extends ChunkDataPCM<Short> {

    /**
     * Samples data
     */
    private short[][] lSamples;

    /**
     *
     * @param DataChunkID
     * @param DataChunkSize
     * @param wBitsPerSample
     * @param wChannels
     * @param dis
     * @throws IOException
     *
     * @since 2013-08-27
     */
    ChunkDataPCMShort(String DataChunkID, int DataChunkSize, short wBitsPerSample,
            short wChannels, DataInputStream dis) throws IOException {
        super(DataChunkID, DataChunkSize);
        nChannels = wChannels;
        nSamples = (ckSize / nChannels) / (wBitsPerSample / 8);
        readSamples(dis);
    }

    /**
     *
     * @param DataChunkSize
     * @param wBitsPerSample
     * @param wChannels
     * @param dis
     * @throws IOException
     *
     * @since 2013-08-30
     */
    ChunkDataPCMShort(int DataChunkSize, short wBitsPerSample, short wChannels,
            DataInputStream dis) throws IOException {
        super(DataChunkSize);
        nChannels = wChannels;
        nSamples = (ckSize / nChannels) / (wBitsPerSample / 8);
        readSamples(dis);
    }

    /**
     *
     * @param sample_data
     * @param lenght
     * @param wChannels
     *
     * @since 2013-09-02
     */
    ChunkDataPCMShort(short[][] sample_data, int lenght, short wChannels) {
        super(2 * wChannels * lenght);
        nChannels = wChannels;
        nSamples = lenght;
        lSamples = new short[wChannels][lenght];
        for (int i = 0; i < wChannels; i++) {
            for (int j = 0; j < lenght; j++) {
                lSamples[i][j] = sample_data[i][j];
            }
        }
    }

    /**
     *
     * @return
     * @since 2013-08-30
     */
    @Override
    Short getMaxSample() {
        short result = 0;
        short temp = 0;
        for (int i = 0; i < nSamples; i++) {
            for (int j = 0; j < nChannels; j++) {
                temp = (short) Math.abs(lSamples[j][i]);
                if (temp > result) {
                    result = temp;
                }
            }
        }
        return result;
    }

    /**
     *
     * @param dis
     * @throws IOException
     * @since 2013-08-30
     */
    @Override
    void readSamples(DataInputStream dis) throws IOException {
        lSamples = new short[nChannels][nSamples];
        for (int i = 0; i < nSamples; i++) {
            for (int j = 0; j < nChannels; j++) {
                lSamples[j][i] = readShortLE(dis);
            }
        }
    }

    /**
     *
     * @param wChannel
     * @param dwPosition
     * @return
     * @since 2013-08-30
     */
    @Override
    Short getSample(short wChannel, int dwPosition) {
        return lSamples[wChannel - 1][dwPosition];
    }

    /**
     *
     * @param wChannel
     * @param dwPosition
     * @param wSample
     * @since 2013-08-30
     */
    @Override
    void setSample(short wChannel, int dwPosition, Short wSample) {
        lSamples[wChannel - 1][dwPosition] = wSample;
    }

    /**
     *
     * @param start
     * @param size
     * @param _Channel
     * @param lframe
     * @since 2013-08-30
     */
    @Override
    void getFrame(int start, short size, short _Channel, List<Short> lframe) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     *
     * @param start
     * @param lframe
     * @param _Channel
     * @since 2013-08-30
     */
    @Override
    void setFrame(int start, List<Short> lframe, short _Channel) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     *
     * @param dos
     * @throws IOException
     * @since 2013-08-30
     */
    @Override
    void save(DataOutputStream dos) throws IOException {
        writeCkID(dos);
        writeCkSize(dos);
        writeSamples(dos);
    }

    /**
     *
     * @param dos
     * @throws IOException
     * @since 2013-08-30
     */
    @Override
    void writeSamples(DataOutputStream dos) throws IOException {
        for (int i = 0; i < nSamples; i++) {
            for (int j = 0; j < nChannels; j++) {
                writeShortLE(lSamples[j][i], dos);
            }
        }
    }
}
